from selenium.webdriver.common.by import By
from web_study.web_litemall_po.page_object.base_page import BasePage
from web_study.web_litemall_po.utils.log_utils import logger


# 类目列表
class CategoryListPage(BasePage):

    __BTN_ADD = (By.XPATH, "//*[@class='el-icon-edit']")
    __MSG_ADD_OPERATE = (By.XPATH, "//p[contains(text(), '创建')]")
    __MSG_DELETE_OPERATE = (By.XPATH, "//p[contains(text(), '删除')]")
    """类目列表页面：点击添加"""
    def click_add(self):
        logger.info("类目列表页面：点击添加")
        # 点击”添加“按钮
        self.do_find(self.__BTN_ADD).click()
        # self.driver.find_element(By.XPATH, "//*[@class='el-icon-edit']").click()
        # ==》创建类目页面
        from web_study.web_litemall_po.page_object.category_create_page import CategoryCreatePage
        return CategoryCreatePage(self.driver)

    """类目列表页面：获取操作结果"""
    def get_operate_result(self):
        logger.info("类目列表页面：获取创建操作结果")
        # 获取冒泡消息文本
        # element = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(
        #         (By.XPATH, "//p[contains(text(), '创建')]")))
        element = self.wait_element_until_visible(self.__MSG_ADD_OPERATE)
        # 消息文本
        msg = element.text
        logger.info(f"冒泡消息是：{msg}")
        # ==》返回消息文本
        return msg

    """类目列表页面：点击删除"""
    def delete_category(self, category_name):
        logger.info("类目列表页面：点击删除")
        # 对指定的类目进行删除
        self.do_find(By.XPATH, f"//*[text()='{category_name}']/../..//*[text()='删除']").click()

        # ==》跳转到当前页面
        return CategoryListPage(self.driver)

    """类目列表页面：获取删除操作结果"""
    def get_delete_result(self):
        logger.info("类目列表页面：获取删除操作结果")
        # 获取冒泡消息文本
        element = self.wait_element_until_visible(self.__MSG_DELETE_OPERATE)
        # 消息文本
        msg = element.text
        logger.info(f"冒泡消息是：{msg}")
        # ==》返回消息文本
        return msg